package org.groupg.practice;

import com.google.common.util.concurrent.RateLimiter;

public class RateLimiterExample {
    public static void main(String[] args) {
        // 每秒允许 2 个请求
        RateLimiter rateLimiter = RateLimiter.create(2.1);

        for (int i = 0; i < 10; i++) {
            // 尝试获取令牌
            boolean acquired = rateLimiter.tryAcquire();
            if (acquired) {
                System.out.println("Request " + (i + 1) + " is allowed");
            } else {
                System.out.println("Request " + (i + 1) + " is blocked");
            }
        }
    }
}